package seismosurfer.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;

/**
 * A utility class that provides 
 * operations that couldn`t fit
 * anywhere else.
 *
 */
public class Util {
    
    /**
     * Checks an object if it is null.
     * 
     * @param o the Object to be checked
     * @return true if the object is null
     */
    public static boolean isNull(Object o) {
        return (o == null);
    }
    
    /**
     * Checks a String if it is null or blank.
     * 
     * @param s the String to be checked
     * @return true if the String is null or blank
     */
    public static boolean isNullOrBlank(String s) {
        return ((s == null) || (s.trim().equals("")));
    }
    
    /**
     * Determines if a number is even.
     * 
     * @param number the number to be checked
     * @return true if it is even
     */
    public static boolean isEven(long number) {
        return ((number % 2) == 0);
    }
    
    /**
     * Closes an InputStream.
     * 
     * @param in the InputStream to be closed
     */
    public static void close(InputStream in) {
        try {
            in.close();
        } catch (IOException ex) {
            throw new SeismoException(ex);
        }
    }
    
    /**
     * Closes a Reader character stream.
     * 
     * @param in the Reader object to be closed
     */
    public static void close(Reader in) {
        try {
            in.close();
        } catch (IOException ex) {
            throw new SeismoException(ex);
        }
    }
    
    /**
     * Closes a Writer character stream.
     * 
     * @param out the Writer object to be closed
     */
    public static void close(Writer out) {
        try {
            out.close();
        } catch (IOException ex) {
            throw new SeismoException(ex);
        }
    }
    
    /**
     * Gets the filename extension.
     * 
     * @param f the filename for which we want the extension
     * @return the extension of the filename
     */
    public static String getExtension(File f) {
        String ext = null;
        String s = f.getName();
        int i = s.lastIndexOf('.');

        if (i > 0 && i < s.length() - 1) {
            ext = s.substring(i + 1).toLowerCase();
        }
        return ext;
    }
    
    /**
     * Closes an OutputStream output stream of bytes.
     * 
     * @param out the OutputStream object to be closed
     */
    public static void close(OutputStream out) {
        try {
            out.close();
        } catch (IOException ex) {
            throw new SeismoException(ex);
        }
    }
    
    /**
     * Creates a PrintWriter for a given file.
     * 
     * @param f the File for which a PrintWriter object will be created
     * @return a PrintWriter object
     * @throws IOException 
     */
    public static PrintWriter getBuffWriter(File f) throws IOException {
        return new PrintWriter(new BufferedWriter(new FileWriter(f)));
    }
    
    /**
     * Creates a PrintWriter for a given file path.
     * 
     * @param path the file path for which a PrintWriter object will be created
     * @return a PrintWriter object
     * @throws IOException 
     */
    public static PrintWriter getBuffWriter(String path) throws IOException {
        return new PrintWriter(new BufferedWriter(new FileWriter(path)));
    }

}